package com.shujia.controller;

import com.shujia.bean.Student;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;

@RestController
public class PhoneixController {


    private static Connection connection;

    static {
        //创建连接
        try {
            connection = DriverManager.getConnection("jdbc:phoenix:master,node1,node2:2181");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/queryById")
    public Student queryById(String id) {
        Student student = new Student();

        try {
            PreparedStatement stat = connection.prepareStatement("select * from STUDENT where id=?");

            stat.setString(1, id);

            ResultSet resultSet = stat.executeQuery();

            if (resultSet.next()) {
                String name = resultSet.getString("name");
                String age = resultSet.getString("age");
                String gender = resultSet.getString("gender");
                String clazz = resultSet.getString("clazz");


                student.setId(id);
                student.setName(name);
                student.setAge(Integer.parseInt(age));
                student.setGender(gender);
                student.setClazz(clazz);

            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return student;
    }


}
